package com.example.leoss;

import android.Manifest;
import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.pm.ActivityInfo;
import android.content.pm.PackageManager;
import android.support.annotation.IdRes;
import android.support.annotation.NonNull;
import android.support.design.widget.BottomNavigationView;
import android.support.v4.app.ActivityCompat;
import android.support.v4.view.ViewPager;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.MenuItem;
import android.view.View;
import android.view.WindowManager;
import android.widget.Toast;

import com.example.leoss.denglu_zhuce.DengluActivity;
import com.example.leoss.zhuye.BottomNavigationViewHelper;
import com.example.leoss.zhuye.FragmentAdapter;
import com.example.leoss.zhuye.GerenFragment;
import com.example.leoss.zhuye.ShouyeFragment;
import com.example.leoss.zhuye.SousuoFragment;

public class ShouyeActivity extends AppCompatActivity  {
    private BottomNavigationView navigationView;
    private MenuItem menuItem;
    boolean yindao=true;

    private static final int REQUEST_EXTERNAL_STORAGE = 1;
    /**
     * 权限获取
     */
    private static String[] PERMISSIONS_STORAGE = {
            Manifest.permission.READ_EXTERNAL_STORAGE,
            Manifest.permission.WRITE_EXTERNAL_STORAGE,
            Manifest.permission.MOUNT_UNMOUNT_FILESYSTEMS};
    private ViewPager viewparge;
    private FragmentAdapter mFragmentAdapter;
    SharedPreferences sp;





    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_shouye);

        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN);
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);//竖屏

         sp=getSharedPreferences("yindao",MODE_PRIVATE);
         yindao=sp.getBoolean("yd",true);


        ActionBar actionBar=getSupportActionBar();
        if (actionBar!=null){
            actionBar.hide();
        }

        if (yindao){
            startActivity(new Intent(ShouyeActivity.this,Yindao.class));

        }


        SharedPreferences sp=getSharedPreferences("confi",MODE_PRIVATE);
        Boolean denglu=sp.getBoolean("denglu",false);

        if (denglu==false)
        {
            startActivity(new Intent(ShouyeActivity.this,DengluActivity.class));
        }
        else
        {
            Toast.makeText(ShouyeActivity.this,"已自动登录",Toast.LENGTH_SHORT).show();
        }





        verifyStoragePermissions(ShouyeActivity.this);
        initFragment();

    }

    public static void verifyStoragePermissions(Activity activity)
    {
        int permission = ActivityCompat.checkSelfPermission(activity,
                Manifest.permission.ACCESS_FINE_LOCATION);

        if (permission != PackageManager.PERMISSION_GRANTED)
        {
            ActivityCompat.requestPermissions(activity, PERMISSIONS_STORAGE,
                    REQUEST_EXTERNAL_STORAGE);

        }


    }

    /**
     * 重复点击返回
     */
    private long exitTime = 0;
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event)
    {
        if(keyCode == KeyEvent.KEYCODE_BACK && event.getAction() == KeyEvent.ACTION_DOWN)//event.getActivity()==KeyEvent.ACTION_DOWN
        {
            if((System.currentTimeMillis()-exitTime) > 2000)
            {
                Toast.makeText(getApplicationContext(), "再按一次退出程序", Toast.LENGTH_SHORT).show();

                exitTime = System.currentTimeMillis();

            } else
            {
                Intent home = new Intent(Intent.ACTION_MAIN);
                home.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                home.addCategory(Intent.CATEGORY_HOME);

                startActivity(home);

            }
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }


   /**
     * 初始化
     */
    private void initFragment()
    {
        navigationView =  findViewById(R.id.bnv_main);
        viewparge=findViewById(R.id.fl_main);


        navigationView.setItemIconTintList(null);

        viewparge.setAdapter(mFragmentAdapter);
        viewparge.setOffscreenPageLimit(3);
        navigationView.getMenu().findItem(R.id.navigation_sousuo2).setIcon(R.drawable.img_sousuo);
        navigationView.getMenu().findItem(R.id.navigation_geren2).setIcon(R.drawable.img_geren);
        BottomNavigationViewHelper.disableShiftMode(navigationView);
        navigationView.setOnNavigationItemSelectedListener(
                new BottomNavigationView.OnNavigationItemSelectedListener() {
                    @Override
                    public boolean onNavigationItemSelected(@NonNull MenuItem item) {
                        switch (item.getItemId()) {
                            case R.id.navigation_shouye2:
                                viewparge.setCurrentItem(0);
                                navigationView.getMenu().findItem(R.id.navigation_shouye2).setIcon(R.drawable.img_zhuye2);
                                navigationView.getMenu().findItem(R.id.navigation_sousuo2).setIcon(R.drawable.img_sousuo);
                                navigationView.getMenu().findItem(R.id.navigation_geren2).setIcon(R.drawable.img_geren);break;

                            case R.id.navigation_sousuo2:
                                viewparge.setCurrentItem(1);
                                navigationView.getMenu().findItem(R.id.navigation_shouye2).setIcon(R.drawable.img_zhuye);
                                navigationView.getMenu().findItem(R.id.navigation_sousuo2).setIcon(R.drawable.img_sousuo2);
                                navigationView.getMenu().findItem(R.id.navigation_geren2).setIcon(R.drawable.img_geren);break;
                            case R.id.navigation_geren2:
                                viewparge.setCurrentItem(2);
                                navigationView.getMenu().findItem(R.id.navigation_shouye2).setIcon(R.drawable.img_zhuye);
                                navigationView.getMenu().findItem(R.id.navigation_sousuo2).setIcon(R.drawable.img_sousuo);
                                navigationView.getMenu().findItem(R.id.navigation_geren2).setIcon(R.drawable.img_geren2);break;

                        }
                        return false;
                    }
                });

        viewparge.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                if (menuItem != null) {
                    menuItem.setChecked(false);
                } else {
                    navigationView.getMenu().getItem(0).setChecked(false);
                }

                menuItem = navigationView.getMenu().getItem(position);
                menuItem.setChecked(true);
                switch (position){
                    case 0:
                        menuItem.setIcon(R.drawable.img_zhuye2);
                        navigationView.getMenu().findItem(R.id.navigation_sousuo2).setIcon(R.drawable.img_sousuo);
                        navigationView.getMenu().findItem(R.id.navigation_geren2).setIcon(R.drawable.img_geren);break;
                    case 1:
                        navigationView.getMenu().findItem(R.id.navigation_shouye2).setIcon(R.drawable.img_zhuye);
                        navigationView.getMenu().findItem(R.id.navigation_sousuo2).setIcon(R.drawable.img_sousuo2);
                        navigationView.getMenu().findItem(R.id.navigation_geren2).setIcon(R.drawable.img_geren);break;
                    case 2:
                        navigationView.getMenu().findItem(R.id.navigation_shouye2).setIcon(R.drawable.img_zhuye);
                        navigationView.getMenu().findItem(R.id.navigation_sousuo2).setIcon(R.drawable.img_sousuo);
                        navigationView.getMenu().findItem(R.id.navigation_geren2).setIcon(R.drawable.img_geren2);break;
                }


            }



            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });

        //禁止ViewPager滑动
//        viewPager.setOnTouchListener(new View.OnTouchListener() {
//            @Override
//            public boolean onTouch(View v, MotionEvent event) {
//                return true;
//            }
//        });

        setupViewPager(viewparge);

    }


    private void setupViewPager(ViewPager viewPager) {
        FragmentAdapter adapter = new FragmentAdapter(getSupportFragmentManager());

        adapter.addFragment(ShouyeFragment.newInstance("首页"));
        adapter.addFragment(SousuoFragment.newInstance("通讯录"));
        adapter.addFragment(GerenFragment.newInstance("我的"));
        viewPager.setAdapter(adapter);
    }




    /**
     * 重新配置每个按钮的图标
     */
    private void resetToDefaultIcon() {
        navigationView.getMenu().findItem(R.id.navigation_shouye2).setIcon(R.drawable.img_zhuye);
        navigationView.getMenu().findItem(R.id.navigation_sousuo2).setIcon(R.drawable.img_sousuo);
        navigationView.getMenu().findItem(R.id.navigation_geren2).setIcon(R.drawable.img_geren);
    }


}
